﻿////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	Convert
//	converte i files *.shp e *.dbf i files *.txt (mappe del FVG)
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.Drawing;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class convert
    {
        public struct Fieldt
        {
            public string name;
            public char type;
            public int length;
            public int dec;
        }

        public static Fieldt[] vecT = new Fieldt[20];
        public convert()
        {
            //           InitializeComponent();
        }


        public void convert_dbf1(string fileName, string target)
        {
            int ipos = fileName.IndexOf('.');
            string ss = fileName.Substring(0, ipos);
            string file_r = string.Concat(ss, ".dbf");

            string path = FormIniz.quadroUnione5000;
            string dbf = string.Concat(path, target, "_dbf.txtx");
            int iret = convertDbf_txt(file_r, dbf);
            if (iret != 0) MessageBox.Show("errore:  convertDbf_txt  " + iret.ToString());


        }

        private int convertDbf_txt(string file_r, string file_w)
        {
            int ifile = 0;
            if (file_w.Contains("area")) ifile = 1;
            if (file_w.Contains("line")) ifile = 2;
            if (file_w.Contains("point")) ifile = 3;
            if (file_w.Contains("text")) ifile = 4;
            if (ifile == 0) return (1);

            if (File.Exists(file_w))
            {
                File.Delete(file_w);
            }
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            StreamWriter fs = new StreamWriter(file_w);
            if (File.Exists(file_r))
            {
                int icomp = 1;
                using (BinaryReader reader = new BinaryReader(File.Open(file_r, FileMode.Open)))
                {
                    byte bt = reader.ReadByte();        //  numero versione
                    byte[] bts = reader.ReadBytes(3);       //data ultima modifica
                    int numRec = reader.ReadInt32();
                    string ss = "numero records: ";
                    ss = string.Concat(ss, numRec.ToString(), "\n");
                    fs.Write(ss);
                    int lengthHeader = reader.ReadInt16();  //  length of header structure
                    int lengthRec = reader.ReadInt16();  //  length of each record + 1
                    reader.ReadBytes(20);
                    int no_fields = (lengthHeader - 33) / 32;
                    for (int i = 0; i < no_fields; ++i)
                    {
                        int j;
                        bts = reader.ReadBytes(11);
                        for (j = 0; j < 11; ++j)
                        {
                            if (bts[j] == 0) break;
                        }
                        byte[] bte = new byte[j];
                        Buffer.BlockCopy(bts, 0, bte, 0, j);

                        ss = System.Text.Encoding.UTF7.GetString(bte);

                        //           MessageBox.Show(j.ToString() + "  " + ss.Length.ToString());

                        vecT[i].name = string.Copy(ss);

                        if (ifile == 1 && i == 0)
                        {
                            icomp = string.Compare(vecT[i].name, "ID");
                            if (icomp == 0) ifile = 5;
                        }

                        bt = reader.ReadByte();
                        vecT[i].type = (char)bt;
                        int fieldDataAddress = reader.ReadInt32();
                        byte fieldlength = reader.ReadByte();
                        vecT[i].length = fieldlength;
                        byte decimalCount = reader.ReadByte();
                        vecT[i].dec = decimalCount;

                        reader.ReadBytes(14);
                    }
                    reader.ReadBytes(1);

                    for (int i = 0; i < numRec; ++i)
                    {
                        string ss1 = string.Empty;
                        string ss3 = string.Empty;
                        string ss4 = string.Empty;
                        bt = reader.ReadByte();
                        byte[] bts1 = reader.ReadBytes(lengthRec - 1);
                        int k, nWidth;
                        int ipnt = 0;

                        for (int j = 0; j < no_fields; ++j)
                        {
                            if (ifile == 1)
                            {
                                if (j == 0)
                                {
                                    ipnt = 0;
                                    nWidth = 10;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, 0, bte, 0, nWidth);
                                    ss1 = System.Text.Encoding.UTF7.GetString(bte);
                                    ss1 = string.Concat(ss1, "#");
                                    ipnt += 255;
                                }

                                if (j == 1)
                                {
                                    for (k = 0; k < 255; ++k)
                                    {
                                        if (bts1[k + ipnt] == 0) break;
                                    }
                                    nWidth = 80;
                                    //                             if (k < nWidth) nWidth = k;

                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 2)
                                {
                                    for (k = 0; k < 255; ++k)
                                    {
                                        if (bts1[k + ipnt] == 0) break;
                                    }
                                    nWidth = 20;
                                    if (k < nWidth) nWidth = k;

                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 3)
                                {
                                    ipnt += 255;
                                }
                                if (j == 4)
                                {
                                    ipnt += 255;
                                }
                                if (j == 5 || j == 6)
                                {
                                    nWidth = 8;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 7 || j == 8)
                                {
                                    nWidth = 24;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    string ss2 = Encoding.UTF7.GetString(bte);
                                    float flt = float.Parse(ss2, nfi);
                                    ss3 = flt.ToString("F2", nfi);
                                    int length = ss3.Length;
                                    if (length < 10)
                                    {
                                        ss3 = ss3.PadLeft(10, ' ');
                                    }
                                    ss1 = string.Concat(ss1, ss3, "#");
                                    ipnt += 24;
                                }
                                if (j == 9)
                                {
                                    nWidth = 10;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    string ss2 = Encoding.UTF7.GetString(bte);
                                    ss1 = string.Concat(ss2, "#", ss1);
                                }
                            }


                            if (ifile == 2)
                            {
                                if (j == 0)
                                {
                                    ipnt = 0;
                                    nWidth = 10;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    string ss2 = Encoding.UTF7.GetString(bte);
                                    ss1 = string.Concat(ss1, ss2, "#");
                                    ipnt += 10;
                                }
                                if (j == 1)
                                {
                                    nWidth = 10;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    string ss2 = Encoding.UTF7.GetString(bte);
                                    ss1 = string.Concat(ss1, ss2, "#");
                                    ipnt += 255;
                                }

                                if (j == 2)
                                {
                                    for (k = 0; k < 255; ++k)
                                    {
                                        if (bts1[k + ipnt] == 0) break;
                                    }
                                    nWidth = 80;
                                    //                             if (k < nWidth) nWidth = k;

                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 3)
                                {
                                    for (k = 0; k < 255; ++k)
                                    {
                                        if (bts1[k + ipnt] == 0) break;
                                    }
                                    nWidth = 20;
                                    if (k < nWidth) nWidth = k;

                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 4 || j == 5)
                                {
                                    ipnt += 255;
                                }

                                if (j == 6 || j == 7)
                                {
                                    nWidth = 8;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 8 || j == 9)
                                {
                                    nWidth = 24;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    string ss2 = Encoding.UTF7.GetString(bte);
                                    float flt = float.Parse(ss2, nfi);
                                    ss3 = flt.ToString("F2", nfi);
                                    int length = ss3.Length;
                                    if (length < 10)
                                    {
                                        ss3 = ss3.PadLeft(10, ' ');
                                    }
                                    ss1 = string.Concat(ss1, ss3, "#");
                                    ipnt += 24;
                                }
                            }


                            if (ifile == 3)
                            {
                                if (j == 0)
                                {
                                    ipnt = 0;
                                    nWidth = 10;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    string ss2 = Encoding.UTF7.GetString(bte);
                                    ss1 = string.Concat(ss1, ss2, " #");
                                    ipnt += 10;
                                }

                                if (j == 1)
                                {
                                    nWidth = 10;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    string ss2 = Encoding.UTF7.GetString(bte);
                                    ss1 = string.Concat(ss1, ss2, "#");
                                    ipnt += 255;
                                }
                                if (j == 2)
                                {
                                    for (k = 0; k < 255; ++k)
                                    {
                                        if (bts1[k + ipnt] == 0) break;
                                    }
                                    nWidth = 80;
                                    //                             if (k < nWidth) nWidth = k;

                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 3)
                                {
                                    for (k = 0; k < 255; ++k)
                                    {
                                        if (bts1[k + ipnt] == 0) break;
                                    }
                                    nWidth = 20;
                                    if (k < nWidth) nWidth = k;

                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 4 || j == 5)
                                {
                                    ipnt += 255;
                                }

                                if (j == 6 || j == 7)
                                {
                                    nWidth = 8;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 8 || j == 9 || j == 10 || j == 11)
                                {
                                    nWidth = 24;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    for (k = 0; k < nWidth; ++k)
                                    {
                                        if (bte[k] != 32) break;
                                    }
                                    if (k < nWidth)
                                    {
                                        string ss2 = Encoding.UTF7.GetString(bte);
                                        float flt = float.Parse(ss2, nfi);
                                        ss3 = flt.ToString("F2", nfi);
                                    }
                                    else
                                    {

                                        ss3 = "0.00";
                                    }
                                    int length = ss3.Length;
                                    if (length < 10)
                                    {
                                        ss3 = ss3.PadLeft(10, ' ');
                                    }
                                    ss1 = string.Concat(ss1, ss3, "#");
                                    ipnt += 24;
                                }
                            }
                            if (ifile == 4)
                            {
                                if (j == 0)
                                {
                                    ipnt = 0;
                                    nWidth = 10;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    string ss2 = Encoding.UTF7.GetString(bte);
                                    ss1 = string.Concat(ss1, ss2, "#");
                                    ipnt += 10;
                                }

                                if (j == 1)
                                {
                                    nWidth = 10;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    string ss2 = Encoding.UTF7.GetString(bte);
                                    ss1 = string.Concat(ss1, ss2, "#");
                                    ipnt += 255;
                                }
                                if (j == 2)
                                {
                                    for (k = 0; k < 255; ++k)
                                    {
                                        if (bts1[k + ipnt] == 0) break;
                                    }
                                    nWidth = 80;
                                    //                             if (k < nWidth) nWidth = k;

                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 3)
                                {
                                    for (k = 0; k < 255; ++k)
                                    {
                                        if (bts1[k + ipnt] == 0) break;
                                    }
                                    nWidth = 20;
                                    if (k < nWidth) nWidth = k;

                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 4 || j == 5)
                                {
                                    ipnt += 255;
                                }

                                if (j == 6 || j == 7)
                                {
                                    nWidth = 8;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }

                                if (j == 8)
                                {
                                    nWidth = 50;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }

                                if (j == 9 || j == 10)
                                {
                                    nWidth = 24;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    for (k = 0; k < nWidth; ++k)
                                    {
                                        if (bte[k] != 32) break;
                                    }
                                    if (k < nWidth)
                                    {
                                        string ss2 = Encoding.UTF7.GetString(bte);
                                        float flt = float.Parse(ss2, nfi);
                                        ss3 = flt.ToString("F2", nfi);
                                    }
                                    else
                                    {

                                        ss3 = "0.00";
                                    }
                                    int length = ss3.Length;
                                    if (length < 10)
                                    {
                                        ss3 = ss3.PadLeft(10, ' ');
                                    }
                                    ss1 = string.Concat(ss1, ss3, "#");
                                    ipnt += 24;

                                }

                                if (j == 11 || j == 12)
                                {
                                    nWidth = 5;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    string ss2 = Encoding.UTF7.GetString(bte);
                                    ss1 = string.Concat(ss1, ss2, "#");
                                    ipnt += 5;
                                }

                                if (j == 13)
                                {
                                    nWidth = 24;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    for (k = 0; k < nWidth; ++k)
                                    {
                                        if (bte[k] != 32) break;
                                    }
                                    if (k < nWidth)
                                    {
                                        string ss2 = Encoding.UTF7.GetString(bte);
                                        float flt = float.Parse(ss2, nfi);
                                        ss3 = flt.ToString("F3", nfi);
                                    }
                                    else
                                    {

                                        ss3 = "0.000";
                                    }
                                    int length = ss3.Length;
                                    if (length < 10)
                                    {
                                        ss3 = ss3.PadLeft(10, ' ');
                                    }
                                    ss1 = string.Concat(ss1, ss3, "#");
                                    ipnt += 24;
                                }
                            }

                            if (ifile == 5)
                            {
                                if (j == 0)
                                {
                                    nWidth = 10;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = Encoding.UTF7.GetString(bte);
                                    ss1 = string.Concat(ss1, "#");
                                    ipnt += 10;
                                }

                                if (j == 1)
                                {
                                    nWidth = 10;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss4 = System.Text.Encoding.UTF7.GetString(bte);
                                    ss1 = string.Concat(ss1, ss4, "#");
                                    ipnt += 255;
                                }

                                if (j == 2)
                                {
                                    for (k = 0; k < 255; ++k)
                                    {
                                        if (bts1[k + ipnt] == 0) break;
                                    }
                                    nWidth = 80;
                                    //                             if (k < nWidth) nWidth = k;

                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 3)
                                {
                                    for (k = 0; k < 255; ++k)
                                    {
                                        if (bts1[k + ipnt] == 0) break;
                                    }
                                    nWidth = 20;
                                    if (k < nWidth) nWidth = k;

                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                if (j == 4)
                                {
                                    ipnt += 255;
                                }
                                if (j == 5)
                                {
                                    ipnt += 255;
                                }
                                if (j == 6 || j == 7)
                                {
                                    nWidth = 8;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "#");
                                    ipnt += 255;
                                }
                                //                              MessageBox.Show("ss1 " + ss1);
                                if (j == 8 || j == 9)
                                {
                                    nWidth = 24;
                                    byte[] bte = new byte[nWidth];
                                    Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                    string ss2 = Encoding.UTF7.GetString(bte);
                                    float flt = float.Parse(ss2, nfi);
                                    ss3 = flt.ToString("F2", nfi);
                                    int length = ss3.Length;
                                    if (length < 10)
                                    {
                                        ss3 = ss3.PadLeft(10, ' ');
                                    }
                                    ss1 = string.Concat(ss1, ss3, "#");
                                    ipnt += 24;
                                    //         ss1 = string.Concat(ss1, ss4, "#");
                                }

                            }
                        }

                        ss1 = string.Concat(ss1, "\n");
                        fs.Write(ss1);
                    }

                    reader.Close();
                }

                fs.Close();
            }

            return (0);
        }

        public void convert_shp1(string fileName, string target)
        {
            int ipos = fileName.IndexOf('.');
            string ss = fileName.Substring(0, ipos);
            string file_r = string.Concat(ss, ".shp");
            string file_rx = string.Concat(ss, ".shx");

            string path = FormIniz.quadroUnione5000;
            string shp = string.Concat(path, target, "_shp.txtx");

            int iret = convertShp_txt(file_r, file_rx, shp);
            if (iret != 0) MessageBox.Show("errore:  convertShp_txt  " + iret.ToString());


        }


        private int convertShp_txt(string file_r, string file_rx, string file_w)
        {

            if (File.Exists(file_w))
            {
                File.Delete(file_w);
            }
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            int iline = 0;
            using (BinaryReader reader = new BinaryReader(File.Open(file_rx, FileMode.Open)))
            {
                byte[] bytes = reader.ReadBytes(4);
                Array.Reverse(bytes);
                int fileCode = BitConverter.ToInt32(bytes, 0);
                if (fileCode != 9994) return 1;

                byte[] skip = reader.ReadBytes(20);

                bytes = reader.ReadBytes(4);
                Array.Reverse(bytes);
                int fileLength = BitConverter.ToInt32(bytes, 0);
                int nRec = (fileLength - 50) / 4;

                int version = reader.ReadInt32();
                if (version != 1000) return 2;

                skip = reader.ReadBytes(68);

                for (int irec = 0; irec < nRec; ++irec)   //
                {
                    bytes = reader.ReadBytes(4);
                    //               Array.Reverse(bytes);
                    //             int offset = BitConverter.ToInt32(bytes, 0);

                    bytes = reader.ReadBytes(4);
                    Array.Reverse(bytes);
                    int contentLength = BitConverter.ToInt32(bytes, 0);
                    int nPoints = (contentLength - 40) / 16;
                    iline += nPoints;
                }
            }
            StreamWriter fs = new StreamWriter(file_w);
            if (File.Exists(file_r))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(file_rx, FileMode.Open)))
                {
                    byte[] bytes = reader.ReadBytes(4);
                    Array.Reverse(bytes);
                    int fileCode = BitConverter.ToInt32(bytes, 0);
                    if (fileCode != 9994) return 1;

                    byte[] skip = reader.ReadBytes(20);

                    bytes = reader.ReadBytes(4);
                    Array.Reverse(bytes);
                    int fileLength = BitConverter.ToInt32(bytes, 0);

                    int nRec = (fileLength - 50) / 4;

                    int version = reader.ReadInt32();
                    if (version != 1000) return 2;

                    int nSHPType = reader.ReadInt32();
                    double Xmin = reader.ReadDouble();
                    double Ymin = reader.ReadDouble();
                    double Xmax = reader.ReadDouble();
                    double Ymax = reader.ReadDouble();
                    double Zmin = reader.ReadDouble();
                    double Zmax = reader.ReadDouble();
                    double Mmin = reader.ReadDouble();
                    double Mmax = reader.ReadDouble();

                    string ss = SHPTypeName(nSHPType);
                    if (ss.Contains("UnknownShapeType")) return 3;

                    ss = "Shapefile type: ";
                    ss = string.Concat(ss, SHPTypeName(nSHPType), "  # of Shape: ", nRec.ToString(), "; # of Points: ", iline.ToString());
                    fs.WriteLine(ss);

                    ss = "File Bounds: ";
                    string s1 = Xmin.ToString("F0", nfi);
                    ss = string.Concat(ss, s1, " , ");

                    s1 = Ymin.ToString("F0", nfi);
                    ss = string.Concat(ss, s1, " , ");

                    s1 = Xmax.ToString("F0", nfi);
                    ss = string.Concat(ss, s1, " , ");

                    s1 = Ymax.ToString("F0", nfi);
                    ss = string.Concat(ss, s1, "  ");
                    fs.WriteLine(ss);

                    BinaryReader readerp = new BinaryReader(File.Open(file_r, FileMode.Open));
                    for (int irec = 0; irec < nRec; ++irec)   //
                    {


                        bytes = reader.ReadBytes(4);
                        Array.Reverse(bytes);
                        int offset = BitConverter.ToInt32(bytes, 0);

                        bytes = reader.ReadBytes(4);
                        Array.Reverse(bytes);
                        int contentLength = BitConverter.ToInt32(bytes, 0);




                        byte[] vect = new byte[contentLength];

                        readerp.BaseStream.Position = 2 * offset;

                        bytes = readerp.ReadBytes(4);
                        Array.Reverse(bytes);
                        int rec = BitConverter.ToInt32(bytes, 0);

                        bytes = readerp.ReadBytes(4);
                        Array.Reverse(bytes);
                        int length = BitConverter.ToInt32(bytes, 0);

                        int type = readerp.ReadInt32();
                       int ipnt = 44;
                        if (type == SHPT_ARCZ || type == SHPT_POLYGONZ)
                        {
                            Xmin = readerp.ReadDouble();
                            Ymin = readerp.ReadDouble();
                            Xmax = readerp.ReadDouble();
                            Ymax = readerp.ReadDouble();
                            int nParts = readerp.ReadInt32();
                            int nPoints = readerp.ReadInt32();

                            ss = "Shape:" + irec.ToString() + ", ";
                            ss = string.Concat(ss, SHPTypeName(type));
                            ss = string.Concat(ss, "  nVertices=", nPoints.ToString(), ", nParts=", nParts.ToString());
                            fs.WriteLine(ss);

                            ss = " Bounds: ";
                            s1 = Xmin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Xmax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, "  ");
                            fs.WriteLine(ss);

                            int[] iPart = new int[nParts];
                            for (int j = 0; j < nParts; ++j)
                            {
                                iPart[j] = readerp.ReadInt32();
                                ipnt += 4;
                                //                            if (j > 0) MessageBox.Show(iPart[j].ToString() + "  " + irec.ToString());
                            }
                            if (nParts > 1)
                            {
                                ss = " nParts: ";
                                for (int j = 0; j < nParts; ++j)
                                {
                                    ss = string.Concat(ss, iPart[j], ", ");
                                }
                                fs.WriteLine(ss);
                            }

                            Point[] iPoint = new Point[nPoints];

                            for (int j = 0; j < nPoints; ++j)
                            {
                                iPoint[j].X = (int)readerp.ReadDouble();
                                iPoint[j].Y = (int)readerp.ReadDouble();
                                ss = "    ";
                                ss = string.Concat(ss, iPoint[j].X.ToString() + " ,   " + iPoint[j].Y.ToString() + " ,   ");
                                fs.WriteLine(ss);
                                ipnt += 16;
                            }


                        }

                        if (type == SHPT_MULTIPOINTZ)
                        {
                            Xmin = readerp.ReadDouble();
                            Ymin = readerp.ReadDouble();
                            Xmax = readerp.ReadDouble();
                            Ymax = readerp.ReadDouble();
                            int nPoints = readerp.ReadInt32();
                            int nParts = 0;

                            ss = "Shape:" + irec.ToString() + ", ";
                            ss = string.Concat(ss, SHPTypeName(type));
                            ss = string.Concat(ss, "  nVertices=", nPoints.ToString(), ", nParts=", nParts.ToString());
                            fs.WriteLine(ss);

                            ss = " Bounds: ";
                            s1 = Xmin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Xmax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, "  ");
                            fs.WriteLine(ss);

                            Point[] iPoint = new Point[nPoints];

                            for (int j = 0; j < nPoints; ++j)
                            {
                                iPoint[j].X = (int)readerp.ReadDouble();
                                iPoint[j].Y = (int)readerp.ReadDouble();
                                ss = "    ";
                                ss = string.Concat(ss, iPoint[j].X.ToString() + " ,   " + iPoint[j].Y.ToString() + " ,   ");
                                fs.WriteLine(ss);
                                ipnt += 16;
                            }

                        }



                        //                MessageBox.Show(ipnt.ToString() + "  " + contentLength.ToString() + "  " + length.ToString());
                    }






                    reader.Close();
                    readerp.Close();
                }

                fs.Close();
            }

            return (0);
        }
        private const int SHPT_NULL = 0, SHPT_POINT = 1, SHPT_ARC = 3, SHPT_POLYGON = 5, SHPT_MULTIPOINT = 8;
        private const int SHPT_POINTZ = 11, SHPT_ARCZ = 13, SHPT_POLYGONZ = 15, SHPT_MULTIPOINTZ = 18, SHPT_POINTM = 21;
        private const int SHPT_ARCM = 23, SHPT_POLYGONM = 25, SHPT_MULTIPOINTM = 28, SHPT_MULTIPATCH = 31;

        private string SHPTypeName(int nSHPType)
        {
            switch (nSHPType)
            {
                case SHPT_NULL:
                    return "NullShape";

                case SHPT_POINT:
                    return "Point";

                case SHPT_ARC:
                    return "Arc";

                case SHPT_POLYGON:
                    return "Polygon";

                case SHPT_MULTIPOINT:
                    return "MultiPoint";

                case SHPT_POINTZ:
                    return "PointZ";

                case SHPT_ARCZ:
                    return "ArcZ";

                case SHPT_POLYGONZ:
                    return "PolygonZ";

                case SHPT_MULTIPOINTZ:
                    return "MultiPointZ";

                case SHPT_POINTM:
                    return "PointM";

                case SHPT_ARCM:
                    return "ArcM";

                case SHPT_POLYGONM:
                    return "PolygonM";

                case SHPT_MULTIPOINTM:
                    return "MultiPointM";

                case SHPT_MULTIPATCH:
                    return "MultiPatch";

                default:
                    return "UnknownShapeType";
            }
        }

        public int splitFiles(string direct)
        {

            string ss = FormIniz.nameSect.Substring(0, 5);
            int jj = 1;
            for (int k = 1; k < 5; ++k)
            {
                string name = ss + k.ToString();
                if (ExistElement(name))
                {
                    ViewQ.Selem[jj - 1] = name;
                    ++jj;
                }
            }

            ViewQ.noElem = jj - 1;
            ViewQ.selElem = 0;

            InfoDialog2 dlg = new InfoDialog2();

            DialogResult iret = dlg.ShowDialog();
            if (iret != DialogResult.OK) return (1);
            string selectedElement = ViewQ.Selem[ViewQ.selElem];
            ViewQ.selectedElement = selectedElement;
            FormIniz.nameElement = selectedElement;

            string path = FormIniz.quadroUnione5000;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            char[] delimiterChars = { ':', ',' };
            char[] delimiterChars20 = { ':', ',', '=' };
            char[] delimiterChars2 = { '#' };

            string[] words;
            int count = 0;
            int nPoints = 0;
            int nVertices = 0;
            int nParts = 0;
            int Xmin, Ymin, Xmax, Ymax;
            int XXmin = 0, YYmin = 0, XXmax = 0, YYmax = 0;
            bool bFirst = true;

            string s4 = string.Empty;
            string s5 = string.Empty;

            int opt = 1;

            string file_r = string.Concat(path, "line_dbf.txtx");
            string file_w = string.Concat(path, "line_dbf.txt");
            string file_r1 = string.Concat(path, "line_shp.txtx");
            string file_w1 = string.Concat(path, "line_shp.txt");
            goto L40;

        L10: file_r = string.Concat(path, "text_dbf.txtx");
            file_w = string.Concat(path, "text_dbf.txt");
            file_r1 = string.Concat(path, "text_shp.txtx");
            file_w1 = string.Concat(path, "text_shp.txt");
            opt = 2;
            goto L40;

        L20: file_r = string.Concat(path, "point_dbf.txtx");
            file_w = string.Concat(path, "point_dbf.txt");
            file_r1 = string.Concat(path, "point_shp.txtx");
            file_w1 = string.Concat(path, "point_shp.txt");
            opt = 3;
            goto L40;

        L30: file_r = string.Concat(path, "area_dbf.txtx");
            file_w = string.Concat(path, "area_dbf.txt");
            file_r1 = string.Concat(path, "area_shp.txtx");
            file_w1 = string.Concat(path, "area_shp.txt");
            opt = 4;


        L40: if (File.Exists(file_w)) File.Delete(file_w);
            if (File.Exists(file_w1)) File.Delete(file_w1);

            count = 0;
            nPoints = 0;

            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamReader fr1 = File.OpenText(file_r1);

                s4 = fr.ReadLine();
                s5 = fr1.ReadLine();
                s5 = fr1.ReadLine();

                while (fr.Peek() >= 0)
                {
                    s4 = fr.ReadLine();
                    s5 = fr1.ReadLine();
                    words = s5.Split(delimiterChars20);
                    string s7 = words[3];
                    nVertices = int.Parse(s7);
                    s7 = words[5];
                    nParts = int.Parse(s7);
                    s5 = fr1.ReadLine();
                    if (s4.Contains(selectedElement))
                    {
                        ++count;
                        nPoints += nVertices;
                        words = s5.Split(delimiterChars20);
                        Xmin = int.Parse(words[1]);
                        Ymin = int.Parse(words[2]);
                        Xmax = int.Parse(words[3]);
                        Ymax = int.Parse(words[4]);
                        if (bFirst)
                        {
                            XXmin = Xmin;
                            YYmin = Ymin;
                            XXmax = Xmax;
                            YYmax = Ymax;
                            bFirst = false;
                        }
                        else
                        {
                            if (XXmin > Xmin) XXmin = Xmin;
                            if (YYmin > Ymin) YYmin = Ymin;
                            if (XXmax < Xmax) XXmax = Xmax;
                            if (YYmax < Ymax) YYmax = Ymax;
                        }
                    }
                    if (nParts > 1) s5 = fr1.ReadLine();
                    for (int j = 0; j < nVertices; ++j) s5 = fr1.ReadLine();
                }
                fr.Close();
                fr1.Close();
            }

            if (count > 0)
            {
                StreamReader fr2 = File.OpenText(file_r); //   new StreamReader(file_r);
                StreamReader fr3 = File.OpenText(file_r1);  //new StreamReader(file_r1);
                StreamWriter fw = new StreamWriter(file_w);
                StreamWriter fw1 = new StreamWriter(file_w1);


                s4 = fr2.ReadLine();
                s4 = "numero records: ";
                string sz = count.ToString();
                s4 = string.Concat(s4, sz);
                fw.WriteLine(s4);

                s5 = fr3.ReadLine();
                words = s5.Split(delimiterChars20);


                s5 = "Shapefile type:";
                s5 = string.Concat(s5, words[1], ": ", sz, "; # of Points: ", nPoints.ToString());
                fw1.WriteLine(s5);

                s5 = fr3.ReadLine();
                s5 = "File Bounds: ";
                string s1 = XXmin.ToString("F0", nfi);
                s5 = string.Concat(s5, s1, " , ");

                s1 = YYmin.ToString("F0", nfi);
                s5 = string.Concat(s5, s1, " , ");

                s1 = XXmax.ToString("F0", nfi);
                s5 = string.Concat(s5, s1, " , ");

                s1 = YYmax.ToString("F0", nfi);
                s5 = string.Concat(s5, s1, "  ");
                fw1.WriteLine(s5);
                /* */
                int count1 = 0;
                while (fr2.Peek() >= 0)
                {
                    string s2 = fr2.ReadLine();
                    s5 = fr3.ReadLine();

                    words = s5.Split(delimiterChars20);
                    nVertices = int.Parse(words[3]);
                    s5 = words[5];
                    nParts = int.Parse(s5);

                    if (s2.Contains(selectedElement))
                    {
                        s5 = "Shape:" + count1.ToString() + ", ";
                        s5 = string.Concat(s5, words[2]);

                        s5 = string.Concat(s5, "  nVertices=", words[3], ", nParts=", words[5]);
                        fw1.WriteLine(s5);
                    }

                    s5 = fr3.ReadLine();

                    if (s2.Contains(selectedElement)) fw1.WriteLine(s5);
                    /* */
                    if (s2.Contains(selectedElement))
                    {
                        int ipos = s2.IndexOf('#');
                        int len = s2.Length;
                        string sy = s2.Substring(ipos, len - ipos);
                        ++count1;
                        string sw = count1.ToString();
                        int len1 = sw.Length;
                        int npad = ipos - len1 - 1;
                        for (int i = 0; i < npad; ++i) sw = string.Concat(" ", sw);
                        string sx = string.Concat(sw, sy);
                        fw.WriteLine(sx);



                    }
                    if (nParts > 1)
                    {
                        s5 = fr3.ReadLine();
                        if (s2.Contains(selectedElement)) fw1.WriteLine(s5);
                    }

                    for (int j = 0; j < nVertices; ++j)
                    {
                        s5 = fr3.ReadLine();
                        if (s2.Contains(selectedElement)) fw1.WriteLine(s5);
                    }
                }

                fr2.Close();
                fr3.Close();
                fw.Close();
                fw1.Close();
                File.Delete(file_r);
                File.Delete(file_r1);

                if (opt == 1) goto L10;
                if (opt == 2) goto L20;
                if (opt == 3) goto L30;

            }

            return (0);
        }

        private static bool ExistElement(string elem)
        {
            bool bRet = false;
            using (SQLiteConnection conn = SetConnection1())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    string ss = "select nome from QU_5000 WHERE codice = '";
                    ss = string.Concat(ss, elem, "'");

                    cmd.CommandText = ss;

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows) bRet = true;
                }

                conn.Close();
            }

            return bRet;
        }

        private static SQLiteConnection SetConnection1()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.quadroUnione, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }



    }
}   

    